import {
	HOST
} from "@/common/utils/constants.js";

import store from "@/store/index.js";

async function uploadFile(image) {
	const that = this;
	return new Promise(async function(resolve, reject) {
		uni.uploadFile({
			url: `${HOST}/sys/common/upload`,
			filePath: image,
			name: 'file',
			header: {
				'X-Access-Token': store.state.token
			},
			success: async (uploadFileRes) => {
				if (uploadFileRes.statusCode === 200) {
					const res = JSON.parse(uploadFileRes.data);
					if (!res.success) {
						this.$message.error(`上传失败:${res.message}`);
						reject();
						return;
					}
					const ext = res.message.split('.')[res.message.split('.').length - 1];
					resolve({
						"name": res.message,
						"extname": ext,
						"url": `${HOST}/${res.message}`,
					});
				}
				reject();

			}
		});

	});
}

export default {
	uploadFile
};